﻿#include "logindialog.h"
#include "ui_logindialog.h"
#include <QRegExp>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>

loginDialog::loginDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::loginDialog)
{
    ui->setupUi(this);
    setWindowTitle("Exam_Sys");
    setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint);
    ui->label_bg->setScaledContents(true);

}

loginDialog::~loginDialog()
{
    delete ui;
}

void loginDialog::resizeEvent(QResizeEvent *event)
{
    ui->label_bg->resize(this->width(),this->height());
    QWidget::resizeEvent(event);
}


void loginDialog::on_pushButton_Login_clicked()
{
    QRegExp QQ_mail_style("^[0-9]{6,10}@qq.com");
    bool res=QQ_mail_style.exactMatch(ui->lineEdit_Account->text());
    if(res)
    {
        //QMessageBox::information(this,"提示","匹配成功");
        if(ui->lineEdit_Password->text().isEmpty())
        {
            QMessageBox::information(this,"提示","输入密码不能为空,请重新输入");
            ui->lineEdit_Password->setFocus();
            return ;
        }
        QString account,password;
        account=ui->lineEdit_Account->text();
        password=ui->lineEdit_Password->text();

        QString filename("../account.txt");
        QFile file(filename);
        QTextStream stream(&file);

        if(file.open(QIODevice::ReadOnly|QIODevice::Text))
        {
            QString strline;
            QStringList strList;


            while(!stream.atEnd())
            {
                strline=stream.readLine();
                strList=strline.split(",");

                if(strList.count()!=2)continue;

                if(strList.at(0)==account&&strList.at(1)==password)
                {
                    QMessageBox::information(this,"提示","密码正确,欢迎");
                    done(QDialog::Accepted);
                    file.close();
                    return ;
                }
                else if(strList.at(0)==account&&strList.at(1)!=password)
                {
                    QMessageBox::information(this,"提示","密码错误,请重新输入");
                    ui->lineEdit_Password->clear();
                    ui->lineEdit_Password->setFocus();

                    file.close();
                    return ;
                }
            }

            QMessageBox::information(this,"提示","账户不存在,请重新输入");
            ui->lineEdit_Account->clear();
            ui->lineEdit_Password->clear();
            ui->lineEdit_Account->setFocus();
            file.close();
            return ;

        }
        else
        {
            QMessageBox::information(this,"提示","文件打开失败");
            return ;
        }


    }
    else
    {
        QMessageBox::information(this,"提示","邮箱格式错误,请重新输入");
        ui->lineEdit_Account->clear();
        ui->lineEdit_Password->clear();
        ui->lineEdit_Account->setFocus();
        return ;
    }
}

void loginDialog::on_pushButton_Cancel_clicked()
{
    done(QDialog::Rejected);
}
